package com.ce.framework.auth;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;



public class NetUtil {
	/**
	 * 开发人：andy
	 * 开发日期: 2013-10-26  下午07:36:10
	 * 功能描述: 
	 * 方法的参数和返回值: 
	 * @param args
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws MalformedURLException, IOException {
//		Runtime r = Runtime.getRuntime();
//		System.out.println(r.totalMemory()-r.freeMemory());
//		System.out.println(r.maxMemory());
//		System.out.println(r.freeMemory());
		System.out.println(getContent("http://jw.ykjt.cn/website/AttachAction.do?actionType=download&doKey=658"));
//		JFrame jf = sm.getJFrame();
//		jf.setVisible(true);
//		sm.start();
	}
	
	private static String sessionid="";
	/**
	* 开发人：andy
	* 开发日期: 2013-10-27  上午08:05:03
	* 功能描述: 获取服务心跳信息
	* 方法的参数和返回值: 
	* @param testUrl
	* @return
	* @throws MalformedURLException
	* @throws IOException
	*/
	public static String getContent(String testUrl) throws MalformedURLException, IOException {
		System.out.println("LOAD_URL:"+testUrl);
		URL url = new java.net.URL(testUrl);
		HttpURLConnection con = (HttpURLConnection)url.openConnection(); 
		con.setRequestProperty("User-Agent","Mozilla/4.0");
		con.setRequestProperty("Cookie", sessionid);
//		con.setConnectTimeout(connectTimeout*1000);
//		con.setReadTimeout(readTimeout*1000);
		InputStream in = con.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
		String line = null;
		StringBuffer buff = new StringBuffer();
		while((line = reader.readLine())!=null){
			buff.append(line+"\n");
		}
		reader.close();
		String sid = con.getHeaderField("Set-Cookie");
		if(sid!=null){
			sid = sid.split(";")[0];
		}
		if(sid!=null&&!sid.equals(sessionid)){
			sessionid = sid;
		}
		con.disconnect();
		return buff.toString().trim();
	}

	public static InputStream getInputStream(String testUrl) throws MalformedURLException, IOException {
		System.out.println("LOAD_URL:"+testUrl);
		URL url = new java.net.URL(testUrl);
		HttpURLConnection con = (HttpURLConnection)url.openConnection(); 
		con.setRequestProperty("User-Agent","Mozilla/4.0");
		con.setRequestProperty("Cookie", sessionid);
//		con.setConnectTimeout(connectTimeout*1000);
//		con.setReadTimeout(readTimeout*1000);
		InputStream in = con.getInputStream();
		String sid = con.getHeaderField("Set-Cookie");
		if(sid!=null){
			sid = sid.split(";")[0];
		}
		if(sid!=null&&!sid.equals(sessionid)){
			sessionid = sid;
		}
		return in;
	}

}
